<script setup lang="ts">
import { computed } from 'vue'

interface Props {
  visible: boolean
  title?: string
  content?: string
  itemName?: string
  loading?: boolean
  confirmText?: string
  cancelText?: string
  width?: string | number
  type?: 'warning' | 'error'
}

const props = withDefaults(defineProps<Props>(), {
  title: '确认删除',
  content: '此操作将永久删除该项目，是否继续？',
  loading: false,
  confirmText: '确认删除',
  cancelText: '取消',
  width: 400,
  type: 'warning'
})

const emit = defineEmits<{
  'update:visible': [value: boolean]
  'confirm': []
  'cancel': []
}>()

// 计算属性：对话框显示状态
const dialogVisible = computed({
  get: () => props.visible,
  set: (value) => emit('update:visible', value)
})

// 获取图标和颜色配置
const getTypeConfig = () => {
  switch (props.type) {
    case 'error':
      return {
        icon: 'mdi-alert-circle',
        iconColor: 'error',
        buttonColor: 'error'
      }
    default:
      return {
        icon: 'mdi-alert',
        iconColor: 'warning',
        buttonColor: 'error'
      }
  }
}

const typeConfig = getTypeConfig()

// 确认删除
const handleConfirm = () => {
  emit('confirm')
}

// 取消删除
const handleCancel = () => {
  emit('cancel')
  dialogVisible.value = false
}

// 生成删除内容文本
const deleteContent = computed(() => {
  if (props.itemName) {
    return `确定要删除 "${props.itemName}" 吗？此操作无法撤销。`
  }
  return props.content
})
</script>

<template>
  <VDialog 
    v-model="dialogVisible" 
    :width="width"
    persistent
    class="delete-dialog"
  >
    <VCard class="dialog-card">
      <!-- 内容区域 -->
      <VCardText class="dialog-content">
        <div class="delete-icon-wrapper">
          <VIcon 
            :icon="typeConfig.icon" 
            :color="typeConfig.iconColor" 
            size="48"
            class="delete-icon"
          />
        </div>
        
        <div class="delete-text">
          <h3 class="delete-title">{{ title }}</h3>
          <p class="delete-description">{{ deleteContent }}</p>
        </div>
      </VCardText>

      <VDivider />

      <!-- 操作按钮 -->
      <VCardActions class="dialog-actions">
        <VSpacer />
        <VBtn
          variant="outlined"
          color="grey"
          @click="handleCancel"
          :disabled="loading"
          class="cancel-btn"
        >
          {{ cancelText }}
        </VBtn>
        <VBtn
          :color="typeConfig.buttonColor"
          @click="handleConfirm"
          :loading="loading"
          class="confirm-btn"
        >
          {{ confirmText }}
        </VBtn>
      </VCardActions>
    </VCard>
  </VDialog>
</template>

<style scoped>
.delete-dialog :deep(.v-overlay__content) {
  margin: 24px;
}

.dialog-card {
  border-radius: 16px;
  overflow: hidden;
  text-align: center;
}

.dialog-content {
  padding: 32px 24px 24px;
}

.delete-icon-wrapper {
  margin-bottom: 20px;
}

.delete-icon {
  background: rgba(255, 152, 0, 0.1);
  border-radius: 50%;
  padding: 12px;
  transition: all 0.3s ease;
}

.delete-icon.error {
  background: rgba(244, 67, 54, 0.1);
}

.delete-text {
  max-width: 320px;
  margin: 0 auto;
}

.delete-title {
  font-size: 1.25rem;
  font-weight: 700;
  color: #1e293b;
  margin-bottom: 12px;
  letter-spacing: 0.5px;
}

.delete-description {
  font-size: 0.95rem;
  color: #64748b;
  line-height: 1.6;
  margin: 0;
}

.dialog-actions {
  background: #fafbfc;
  padding: 16px 24px;
  gap: 12px;
  justify-content: center;
}

.cancel-btn,
.confirm-btn {
  border-radius: 8px;
  font-weight: 600;
  text-transform: none;
  min-width: 80px;
  transition: all 0.3s ease;
}

.cancel-btn:hover {
  background: rgba(0, 0, 0, 0.04);
}

.confirm-btn {
  box-shadow: 0 4px 12px rgba(244, 67, 54, 0.25);
}

.confirm-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(244, 67, 54, 0.35);
}

@media (max-width: 600px) {
  .delete-dialog :deep(.v-overlay__content) {
    margin: 12px;
  }
  
  .dialog-content {
    padding: 24px 20px 20px;
  }
  
  .delete-title {
    font-size: 1.125rem;
  }
  
  .delete-description {
    font-size: 0.875rem;
  }
  
  .dialog-actions {
    padding: 12px 20px;
    flex-direction: column-reverse;
    gap: 8px;
  }
  
  .cancel-btn,
  .confirm-btn {
    width: 100%;
  }
}

/* 动画效果 */
.delete-icon {
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
  100% {
    transform: scale(1);
  }
}
</style>
